package com.ty.zls.task;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;

import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.ty.zls.activity.LoginActivity;
import com.ty.zls.bean.Constants;
import com.ty.zls.util.CommonUtil;
import com.ty.zls.util.HttpUtil;
import com.ty.zls.util.StrogeUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class BaseTask extends AsyncTask<Map<String, String>, Void, Map<String, Object>> {


    private String url;
    private QMUITipDialog tipDialog;
    protected Activity context;
    private Handler handler = new Handler();
    private SuccessCallback callback;
    private FailCallback failCallback;

    public void setFailCallback(FailCallback failCallback) {
        this.failCallback = failCallback;
    }

    public BaseTask(String url, Context context, SuccessCallback callback) {
        this.url = url;
        this.context = (Activity) context;
        this.callback = callback;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        tipDialog = new QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("提交中")
                .create();
        tipDialog.show();
    }

    @Override
    protected Map<String, Object> doInBackground(Map<String, String>... maps) {
        Map<String, Object> data = new HashMap<>();
        try {
            JSONObject result = HttpUtil.sendNeedLoginPost(context,maps[0], this.url);
            int code = result.optInt("code");
            data.put("code", code);
            data.put("msg", result.optString("msg"));
            data.put("result", result);
        } catch (Exception e) {
            data.put("code", Constants.ERROR_CODE);
        }
        return data;
    }

    @Override
    protected void onPostExecute(Map<String, Object> map) {
        super.onPostExecute(map);
        tipDialog.dismiss();
        String title = "";
        final Integer code = (Integer) map.get("code");
        if (Constants.ERROR_CODE.equals(code)) {
            if (!CommonUtil.isNetworkAvailable(context)) {
                title = "发送失败,请检查网络是否连接";
            } else {
                title = "发送失败,请稍后重试";
            }
            tipDialog = new QMUITipDialog.Builder(context)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                    .setTipWord(title)
                    .create();
            if(failCallback != null)
                failCallback.excute(title);
        }  else if (Constants.TOKEN_VAILD.equals(code)) {
            //返回登录
            StrogeUtil.rmToken(context);
            context.startActivity(new Intent(context, LoginActivity.class));
            context.finish();
            return;
        } else if (Constants.FAIL_CODE.equals(code)) {
            tipDialog = new QMUITipDialog.Builder(context)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                    .setTipWord((String) map.get("msg"))
                    .create();
            if(failCallback != null)
                failCallback.excute((String) map.get("msg"));
        } else if (Constants.SUCCESS_CODE.equals(code)) {
            tipDialog = null;
            if(callback != null){
                callback.excute((JSONObject) map.get("result"));
            }
        }
        if(tipDialog != null){
            tipDialog.show();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    tipDialog.dismiss();
                }
            }, 1000);
        }
    }


    public interface SuccessCallback {

        void excute(JSONObject result);

    }

    public interface FailCallback {
        void excute(String result);
    }
}
